package com.prs.crm.action.oa.bookmanager;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import sun.misc.Perf.GetPerfAction;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.hr.Resume;
import com.prs.crm.domain.oa.bookmanager.BookRegister;
import com.prs.crm.domain.oa.bookmanager.RevertRegister;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.hr.ResumeService;
import com.prs.crm.service.oa.bookmanager.BookRegisterService;
import com.prs.crm.service.oa.bookmanager.RevertRegisterService;

public class RevertRegisterAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private RevertRegister revertRegister;
	private RevertRegisterService revertRegisterService;
	private PaginationSupport pagination;
	private List<BookRegister> bookRegisters;
	private BookRegisterService bookRegisterService;
	private Collection<Resume> borrowPersons;
	private ResumeService resumeService;
	private Integer[] ids;
	private File upload;
	private String uploadFileName;
	private Application application;
	private ApplicationService ApplicationService;
    
	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public ApplicationService getApplicationService() {
		return ApplicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public RevertRegister getRevertRegister() {
		return revertRegister;
	}

	public void setRevertRegister(RevertRegister revertRegister) {
		this.revertRegister = revertRegister;
	}

	public RevertRegisterService getRevertRegisterService() {
		return revertRegisterService;
	}

	public void setRevertRegisterService(
			RevertRegisterService revertRegisterService) {
		this.revertRegisterService = revertRegisterService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List<BookRegister> getBookRegisters() {
		return bookRegisters;
	}

	public void setBookRegisters(List<BookRegister> bookRegisters) {
		this.bookRegisters = bookRegisters;
	}

	public BookRegisterService getBookRegisterService() {
		return bookRegisterService;
	}

	public void setBookRegisterService(BookRegisterService bookRegisterService) {
		this.bookRegisterService = bookRegisterService;
	}

	public Collection<Resume> getBorrowPersons() {
		return borrowPersons;
	}

	public void setBorrowPersons(Collection<Resume> borrowPersons) {
		this.borrowPersons = borrowPersons;
	}

	public ResumeService getResumeService() {
		return resumeService;
	}

	public void setResumeService(ResumeService resumeService) {
		this.resumeService = resumeService;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	@Action("listRevertRegister")
	public String listRevertRegister() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RevertRegister.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRevertRegisterService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editRevertRegister")
	public String editRevertRegister() {
		this.borrowPersons = this.getResumeService().getAllResume();
		this.bookRegisters = this.getBookRegisterService().getAllBookRegister();
		return SUCCESS;
	}
	@Action("viewRevertRegister")
	public String viewRevertRegister(){
		this.borrowPersons = this.getResumeService().getAllResume();
		this.bookRegisters = this.getBookRegisterService().getAllBookRegister();
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "saveRevertRegister", results = {
			@Result(name = "success", type = "redirect", location = "listRevertRegister"),
			@Result(name = "error", type = "chain", location = "editRevertRegister"), })
	public String saveRevertRegister() {
		if (upload!=null) {
			this.revertRegister.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		this.getRevertRegisterService().save(revertRegister);
		BookRegister br=this.getBookRegisterService().getBookRegisterByName(revertRegister.getBookRegisters().getBookName());
		if (br!=null) {
			Integer nums=br.getNumber()+revertRegister.getNum();
			br.setNumber(nums);
			this.getBookRegisterService().submit(br);
		}
		application = new Application();
		application.setType("一般");
		application.setAppName("[一般]"+"归还登记");
		application.setSqType(2);
		application.setSpr(0);
		application.setPerson(revertRegister.getPerson());
		application.setRevertRegister(revertRegister);
		application.setResign("归还登记");
		this.getApplicationService().save(application);
		return SUCCESS;
	}

	@Action(value = "removeRevertRegister", results = {
			@Result(name = "success", type = "redirect", location ="listRevertRegister"),
			@Result(name = "error", type = "chain", location ="listRevertRegister") })
	public String removeRevertRegister() {
		if (ids == null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getRevertRegisterService().batchRemove(ids);
		return SUCCESS;
	}
}
